package cn.impala.jdbc;

import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
public class ImpalaTest  {
    public static void main(String[] args) throws Exception   {
//impalaurl
        String driver =  "org.apache.hive.jdbc.HiveDriver";
        String driverUrl =  "jdbc:hive2://linux123:25004/default;auth=noSasl";
//sql
        String querySql = "with tmp as (\n" +
                "    select user_id,click_time,(unix_timestamp(click_time)-nvl(unix_timestamp(lag(click_time) over(partition by user_id order by unix_timestamp(click_time))),0))/60 intervals\n" +
                "    from user_clicklog\n" +
                ")\n" +
                "\n" +
                "select user_id,click_time,\n" +
                "        row_number() over(partition by user_id,groupfeild order by unix_timestamp(click_time)) ordernumber\n" +
                "    from(\n" +
                "        select user_id,click_time,\n" +
                "                sum(groupboolean) over(partition by user_id order by unix_timestamp(click_time)) groupfeild\n" +
                "            from (\n" +
                "                select user_id,click_time,\n" +
                "                    case when intervals>30 then 1 else 0  end groupboolean\n" +
                "                from tmp\n" +
                "        )tmp1\n" +
                "    )tmp2 order by unix_timestamp(click_time)\n";
//
//        Class.forName(driver);
        Class.forName("org.apache.hive.jdbc.HiveDriver");
//Drivermanager

        final Connection connection =  DriverManager.getConnection(driverUrl);
        final PreparedStatement ps =  connection.prepareStatement(querySql);
//
        final ResultSet resultSet =  ps.executeQuery();
//
//
        final int columnCount =  resultSet.getMetaData().getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++)  {
                final String string =  resultSet.getString(i);
                System.out.print(string +  "\t");
            }
            System.out.println();
        }
//
        ps.close();
        connection.close();
    }
}
